home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / COMMON.ZIP / FAR2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-09  |  3.8 KB  |  162 lines

  1. #if 0
  2. In article <1995Dec8.143155.4386@nedcu0>, N S GHAI <ghai@imt.unine.ch> wrote:
  3.  
  4. > I have data from an image which is 80Kbytes. So by allocating the memory
  5. > using farmalloc()  I am able to fill the memory array, but when I try to
  6. > access it, the data is not accessable after 64Kbyte.(Because of
  7. > segmentation) .The outline of the code is here:
  8. > char far *mydata;
  9. > main()
  10. > {
  11. > long SIZE=80000;
  12. > char data;
  13. > if((mydata=(char far *)farmalloc(SIZE*sizeof(char))==NULL)
  14. > cout<<"MEMROY ALLOCATION ERROR";
  15. > Imagecopy(image,&mydata); //A function to copy the imagedata to                                
  16. !!! why &mydata?
  17.  
  18. AJR:
  19. I bet Imagecopy is defines as 
  20. Imagecopy(char far *src, char far *dest);
  21. in which case the & isn't needed.
  22.  
  23. > //mydata
  24. > for(long i=0; i < SIZE ;i++ )                                                        
  25. >    data=mydata[i];                 //To acess the data from the array
  26. >                                 //Here data remains valid till 'i'
  27. reaches                                      //65000  after
  28. >                                 //that it is not better.
  29. [snip]
  30.  
  31. In a Microsoft environment, "far" pointers only use 16 bit address
  32. arithmetic on the offset, causing the pointer to wrap at 64K. A "huge"
  33. pointer (not the same as huge memory model) uses 32 bit address
  34. arithmetic.
  35.  
  36. AJR writes:
  37. huge pointer will work, but be horribly slow.
  38. The compiler will re-normalize the pointer everytime you access it.
  39.  
  40. char huge *mydata;
  41. char huge *src;
  42.  
  43.  
  44. // slow using huge pointers, but adequate with todays fast cpu's
  45. Imagecopy(char huge *image, char huge *mydata, long size)
  46. {
  47.    while ( size )
  48.       {
  49.       *mydata++=*src++;
  50.       size--;
  51.       }
  52. }
  53.  
  54.  
  55. void foo(void)
  56. {
  57.    mydata=(char huge *)farmalloc(SIZE);
  58.    if ( mydata )
  59.       {
  60.       // src magiclly set somewhere else
  61.       Imagecopy(src, mydata, SIZE);
  62.       }
  63.    else
  64.       {
  65.       printf("rats! out of mem\n");
  66.       }
  67. }
  68.  
  69. #endif
  70.  
  71. // Now if we want the speed of far pointers we have to do the pointer
  72. // normalization ourselfs.
  73.  
  74.  
  75. #include <stdio.h>
  76. #include <malloc.h>
  77. #include <dos.h>
  78. #include <string.h>
  79.  
  80. #define SIZE 80000lu
  81. #define LESS64K 64000u
  82. // we don't use 65536u because a normalized pointer doesn't 
  83. // have an offset of zero very often
  84.  
  85. char far *mydata;
  86. char far *src;
  87.  
  88. /*
  89. _fmemcpy(unsigned char far *, unsigned char far *, unsigned int size);
  90.  
  91. If in a far memmodel you can just use plain memcpy(), but if in a near mem
  92. model must use a far version of memcpy()
  93.  
  94. in most compilers this is implemented with a rep movs so it is faster than the
  95. c loop used above.
  96.  
  97. */
  98.  
  99.  
  100. /* alter a far pointer so that the offset is zero,
  101.    handy if the data is actualy > 64k long */
  102. /* ---------------------- normalize() -------------------- March 26,1993 */
  103. char far *normalize(char far *p)
  104. {
  105.  
  106.    unsigned short seg,off;
  107.  
  108.     seg = FP_SEG(p);
  109.     off = FP_OFF(p);
  110.     seg += (off>>4);
  111.     off &= 0x000f;
  112.     p = MK_FP(seg,off);
  113.  
  114.    return(p);
  115. }
  116.  
  117. // NOT tested! probably works, but its just to show the technique
  118. // faster using far pointers
  119. void Imagecopy(char far *src, char far *mydata, long size)
  120. {
  121.    unsigned int i;
  122.    while ( size )
  123.       {
  124.       // normalize src and mydata
  125.       src=normalize(src);
  126.       mydata=normalize(mydata);
  127.  
  128.       i=LESS64K;
  129.       if ( size > i )
  130.          {
  131.          // copy 65000
  132.          _fmemcpy(src, mydata, LESS64K);
  133.          src+=LESS64K;
  134.          mydata+=LESS64K;
  135.          size-=LESS64K;
  136.          }
  137.       else
  138.          {
  139.          // copy remainder
  140.          _fmemcpy(src, mydata, size);
  141.          size=0;
  142.          }
  143.  
  144.       }
  145. }
  146.  
  147.  
  148. void foo(void)
  149. {
  150.    mydata=(char far *)farmalloc(SIZE);
  151.    if ( mydata )
  152.       {
  153.       // src magiclly set somewhere else
  154.       Imagecopy(src, mydata, SIZE);
  155.       }
  156.    else
  157.       {
  158.       printf("rats! out of mem\n");
  159.       }
  160. }
  161.